﻿using MyTask.Domain.Models;
using MyTask.Infrastructure.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyTask.Domain.Queries.Handlers
{
    public  class UserInfoQueryHandler
    {
        #region Private variable
        //DbContext
        private readonly MyTaskContext _context;
        #endregion
        #region Constructor
        public UserInfoQueryHandler(MyTaskContext context)
        {
            _context = context;
        }
        #endregion
        #region Public method
        /// <summary>
        /// Log in
        /// </summary>
        /// <param name="loginQuery"></param>
        /// <returns></returns>
        public bool Login(LoginQuery _query)
        {
            var result = _context.UserInfos.Any(p => p.UserName == _query.UserName && p.Password == _query.Password);
            return result;
        }
        /// <summary>
        /// Get a User by UserName
        /// </summary>
        /// <param name="_query"></param>
        /// <returns></returns>
        public UserInfo GetUserInfoByUserName(GetUserInfoByUserNameQuery _query)
        {
            var userInfo = _context.UserInfos.FirstOrDefault(p => p.UserName == _query.UserName);
            return userInfo;
        }
        /// <summary>
        /// Get shared users by user
        /// </summary>
        /// <param name="_query"></param>
        /// <returns></returns>
        public List<UserInfo> GetSharedUsers(GetSharedUsersQuery _query)
        {
            var result = _context.UserInfos.Where(p => p.UserId != _query.UserId).ToList();
            return result;
        }
        
        #endregion
    }
}
